$("head").append('<link rel="stylesheet" href="http://code.jquery.com/ui/1.8.20/themes/base/jquery-ui.css" type="text/css" />');
$("head").append('<link rel="stylesheet" href="https://ftvi.googlecode.com/git/gvt2.css" type="text/css" />');
var politique = ["Benoist Apparu", "Martine Aubry", "Clémentine Autain", "J.-M. Ayrault", "Claude Bartolone", "Frigide Barjot", "François Bayrou", "Pierre Bergé", "J.-L. Borloo", "Delphine Batho", "Najat Belkacem", "Bernard Cazeneuve", "J.-F. Copé", "Bertrand Delanoë", "Gérard Depardieu", "Louis Gallois", "Harlem Désir", "Cécile Duflot", "Laurent Fabius", "Aurélie Filippetti", "Elisabeth Guigou", "Benoit Hamon", "Eva Joly", "Anne Lauvergeon", "Marylise Lebranchu", "J.-Y. Le Drian", "Stéphane Le Foll", "J.-L. Mélenchon", "Arnaud Montebourg", "Pierre Moscovici", "Xavier Niel", "Yannick Noah", "Laurence Parisot", "Fleur Pellerin", "J.-V. Placé", "Vincent Peillon", "Thomas Piketty", "Edwy Plenel", "J.-P. Raffarin", "Ségolène Royal", "François Rebsamen", "Marcel Rufo", "Michel Sapin", "Omar Sy", "Bernard Tapie", "Christiane Taubira", "Lilian Thuram", "Marisol Touraine", "Manuel Valls", "Rama Yade"];
var ministeres = ["Premier ministre", "Intérieur", "Aff. étrangères", "Défense", "Economie", "Budget", "Culture", "Education", "Justice", "Travail", "Redressement productif", "Ecologie", "Agriculture", "Sports", "Logement", "Santé", "Droits des femmes"];
function urlFriendly(a) {
    return a.toLowerCase().replace(/^\s+|\s+$/g, "").replace(/[_|\s]+/g, "-").replace(/[^a-z0-9-]+/g, "").replace(/[-]+/g, "-").replace(/^-+|-+$/g, "")
}
function check() {
    var a = true;
    if ($(".dropzone3").children("ul").children("li").size() == 0) {
        a = false
    }
    $(".dropzone").each(function() {
        if ($(this).children("ul").children("li").size() == 0) {
            a = false
        }
    });
    return a
}
function saveValeur() {
    $("#str_share").attr("value", "");
    $(".ui-droppable").each(function() {
        var a = $(this).attr("id");
        for (var b = 0; b < ministeres.length; b++) {
            if (("dropzone_" + urlFriendly(ministeres[b])) == a) {
                if ($(this).children("ul").children("li").size() != 0) {
                    $("#str_share").attr("value", $("#str_share").attr("value") + "-" + $(this).children("ul").children("li").attr("id"))
                }
            }
        }
    });
    if ($("#str_share").attr("value").substr(0, 1) == "-") {
        $("#str_share").attr("value", $("#str_share").attr("value").substr(1, $("#str_share").attr("value").length - 1))
    }
}
function shareFacebook() {
    if (check()) {
        saveValeur();
        var g = 650;
        var a = 430;
        var f = 0;
        var c = 0;
        var e = "";
        f = ($(window).width() - g) / 2;
        c = ($(window).height() - a) / 2;
        e = "width=" + g + ",height=" + a + ",top=" + c + ",left=" + f + ",location=no,resizable=yes,status=no,titlebar=no,toolbar=no";
        var b = window.location.href;
        if (b.substr(b.length - 1, 1) == "#") {
            b = b.substr(0, b.length - 1)
        }
        b += "#" + $("#str_share").attr("value");
        var d = "https://www.facebook.com/sharer.php?u=" + encodeURIComponent(b) + "&t=" + encodeURIComponent("Coucou, tu veux voir mon gouvernement ?");
        window.open(d, "partage_facebook", e)
    } else {
        alert("Votre gouvernement n'est pas complet.")
    }
}
function shareTwitter() {
    if (check()) {
        saveValeur();
        var g = 650;
        var a = 430;
        var f = 0;
        var c = 0;
        var e = "";
        f = ($(window).width() - g) / 2;
        c = ($(window).height() - a) / 2;
        e = "width=" + g + ",height=" + a + ",top=" + c + ",left=" + f + ",location=no,resizable=yes,status=no,titlebar=no,toolbar=no";
        var h = "Coucou, tu veux voir mon gouvernement ?";
        var b = window.location.href;
        if (b.substr(b.length - 1, 1) == "#") {
            b = b.substr(0, b.length - 1)
        }
        b += "#" + $("#str_share").attr("value");
        var d = "http://twitter.com/share?related=francetvinfo&via=francetvinfo&lang=fr&text=" + encodeURIComponent(h) + "&url=" + encodeURIComponent(b);
        window.open(d, "partage_twitter", e)
    } else {
        alert("Votre gouvernement n'est pas complet.")
    }
}
function raz() {
    window.location.hash = "";
    window.location.href = window.location.href;
    window.location.reload()
}
function generateIhm() {
    var b = "";
    b += '<input type="hidden" id="str_share" value="" />';
    b += '<div class="demo ui-widget ui-helper-clearfix">';
    b += '    <ul id="gallery" class="gallery ui-helper-reset ui-helper-clearfix"></ul>';
    b += '    <div id="container"></div>';
    b += "</div>";
    $(b).appendTo($("#fais_ton_gouvernement"));
    b = "";
    for (var a = 0; a < politique.length; a++) {
        b += '<li id="' + a + '" class="ui-widget-content ui-corner-tr">';
        b += '    <h5 class="ui-widget-header">' + (politique[a]).replace(" ", "<br />") + "</h5>";
        b += '    <img src="http://ftvi.googlecode.com/files/' + urlFriendly(politique[a]) + '.jpg"  alt="' + politique[a] + '" width="70" height="70" />';
        b += "</li>"
    }
    $(b).appendTo($("#gallery"));
    b = "";
    for (a = 0; a < ministeres.length; a++) {
        if (!a) {
            b += '<div id="premier_ministre">';
            b += '    <div id="dropzone_' + urlFriendly(ministeres[a]) + '" class="dropzone3">';
            b += '        <h4 class="ui-widget-header">' + ministeres[a] + "</h4>";
            b += "    </div>";
            b += "</div>"
        } else {
            b += '<div id="dropzone_' + urlFriendly(ministeres[a]) + '" class="dropzone">';
            b += '   <h4 class="ui-widget-header">' + ministeres[a] + "</h4>";
            b += "</div>"
        }
    }
    $(b).appendTo($("#container"));
    b = "";
    b += '<div style="clear: both;"></div>';
    b += '<div class="share">';
    b += '   <img src="http://ftvi.googlecode.com/files/share_facebook3.jpg" id="share_facebook" />';
    b += '   <img src="http://ftvi.googlecode.com/files/share_twitter3.jpg" id="share_twitter" />';
    b += '   <img src="http://ftvi.googlecode.com/files/zero2.jpg" id="raz" />';
    b += "</div>";
    $(b).appendTo($("#container"));
    $("#share_facebook").click(function() {
        shareFacebook()
    });
    $("#share_twitter").click(function() {
        shareTwitter()
    });
    $("#raz").click(function() {
        raz()
    })
}
$(function() {
    generateIhm();
    var e = (window.location.hash.substring(1)).split("&");
    if (e != "") {
        e = ("" + e).split("-");
        for (cpt = 0; cpt < ministeres.length; cpt++) {
            d($("#dropzone_" + urlFriendly(ministeres[cpt])), $("#" + e[cpt]))
        }
        $("#gallery").remove();
        $("#container").css("float", "none");
        $("#container").css("margin", "0 auto");
        $("#container").css("backgroundColor", "#fff");
        $(".share").remove();
        var g = "";
        g += '<div class="share">';
        g += '   <img src="http://ftvi.googlecode.com/files/compoz.jpg" id="composer" />';
        g += "</div>";
        $(g).appendTo($("#container"));
        $("#composer").click(function() {
            raz()
        })
    }
    var a = $("#gallery");
    $("li", a).draggable({
        cancel: "a.ui-icon",
        revert: "invalid",
        containment: $("#demo-frame").length ? "#demo-frame" : "document",
        helper: "clone",
        cursor: "move"
    });
    $(".dropzone3").droppable({
        accept: "#gallery > li",
        activeClass: "ui-state-highlight",
        drop: function(h, i) {
            d($(this), i.draggable)
        }
    });
    $(".dropzone").each(function() {
        $(this).droppable({
            accept: "#gallery > li",
            activeClass: "ui-state-highlight",
            drop: function(h, i) {
                d($(this), i.draggable)
            }
        })
    });
    a.droppable({
        accept: "#container li",
        activeClass: "custom-state-active",
        drop: function(h, i) {
            c(i.draggable)
        }
    });
    var b = "";
    function d(i, h) {
        if (i.children("ul").children("li").size() != 0) {
            i.children("ul").children("li").appendTo(a);
            i.children("ul").children().remove()
        }
        h.fadeOut(function() {
            var j = $("ul", i).length ? $("ul", i) : $("<ul class='gallery ui-helper-reset'/>").appendTo(i);
            h.find("a.ui-icon-trash").remove();
            h.append(b).appendTo(j).fadeIn(function() {})
        })
    }
    var f = "";
    function c(h) {
        h.fadeOut(function() {
            h.find("a.ui-icon-refresh").remove().end().css("width", "70px").append(f).find("img").css("height", "70px").end().appendTo(a).fadeIn()
        })
    }
});